package cn.zhb.backend.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/***
 *@Description 全局拦截器
 *@Author ZHB
 *@Create 2023/9/11 13:53
 **/
@Component
@Slf4j
public class WebInterceptor implements HandlerInterceptor, WebInterceptors {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        //若session存在，填入true 创建新的session，反之返回session
        HttpSession httpSession = request.getSession(false);
        if (httpSession != null) return true;
        //请求无权限，返回状态码提示
        response.setStatus(401);
        return false;
    }
}
